{% extends './layout.html' %} {% block title %} {{ title }} {% endblock %} {% block crumbs %}
<a href="/">首页</a>
<a href="/story">小说管理</a>
<a><cite>添加小说</cite></a>
{% endblock %} {% block body %}
<div class="layout_cont">
	<form class="layui-form" lay-filter="bookForm">
		<div class="layui-form-item">
			<label class="layui-form-label">小说名称</label>
			<div class="layui-input-block">
				<input type="text" name="bookname" lay-verify="required" autocomplete="off" placeholder="请输入小说名称" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">小说作者</label>
			<div class="layui-input-block">
				<input type="text" name="bookauthor" placeholder="请输入小说作者" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">小说类型</label>
			<div class="layui-input-inline">
				<select name="booktype">
					{% for item in types %}
					<option value="{{item.id}}">{{item.typename}}</option>
					{% endfor %}
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">小说封面</label>
			<div class="layui-input-inline" style="width: 240px">
				<input type="text" name="bookimg" placeholder="请输入小说作者" autocomplete="off" class="layui-input">
			</div>
			<div class="layui-input-inline" style="width: 90px;">
				<button type="button" class="layui-btn" id="selectImg"><i class="layui-icon">&#xe64a;</i> 选择</button>
			</div>
			<div class="layui-input-inline">
				<button type="button" class="layui-btn" id="imgUpload"><i class="layui-icon">&#xe67c;</i> 上传图片</button>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">小说状态</label>
			<div class="layui-input-inline">
				<input type="checkbox" name="bookstatus" lay-skin="switch" lay-filter="storyStatus" lay-text="更新中|已完结" checked>
			</div>
		</div>
		<div class="layui-form-item layui-form-text">
			<label class="layui-form-label">小说简介</label>
			<div class="layui-input-block">
				<textarea name="booknotice" placeholder="请输入小说简介" class="layui-textarea"></textarea>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">来源站点</label>
			<div class="layui-input-inline">
				<select name="sourcesite">
					{% for item in sites %}
					<option value="{{item.id}}">{{item.sitename}}</option>
					{% endfor %}
				</select>
			</div>
			<label class="layui-form-label">链接地址</label>
			<div class="layui-input-block" style="margin-left: 420px;">
				<input type="text" name="capturehref" placeholder="请输入获取小说链接地址" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button type="submit" class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
</div>
{% endblock %}{% block scripts %}
<script type="text/javascript">
	layui.use(['layer', 'form', 'upload'], function() {
		var $ = layui.jquery;
		var form = layui.form;
		var upload = layui.upload;
		var layer = layui.layer;
		var bookStatus = 1;
		upload.render({
			elem: '#imgUpload', //绑定元素
			url: '/common/upload/', //上传接口
			acceptMime: 'image/jpg, image/png, image/jpeg',
			done: function(res) {
				//上传完毕回调
				form.val('bookForm', {
					"bookimg": res.data
				});
			},
			error: function() {
				//请求异常回调
			}
		});
		form.on('switch(storyStatus)', function() {
			console.log(this.checked);
			bookStatus = this.checked ? 1 : 0;
		});
		//监听提交
		$('#selectImg').on('click', function() {
			layer.open({
				type: 2,
				title: '选择封面图片',
				move: false,
				area: ['1000px', '660px'],
				content: '/story/getImg',
				btn: ['确定', '取消'],
				yes: function(index, layero) {
					var sonPage = layero.find("iframe")[0].contentWindow.document;
					form.val('bookForm', {
						"bookimg": $(sonPage).find('[name="imgRadio"]:checked').val()
					});
					layer.close(index);
				}
			})
		});
		form.on('submit(add)', function(data) {
			var params = data.field;
			params.bookstatus = bookStatus;
			delete params.file;
			$.post('create', params, function(res) {
				if (res.code == 200 && res.success) {
					window.location.href = '/story'
				} else {
					layer.msg(res.msg);
				}
			})
			return false;
		});

	});
</script>
{% endblock %}
